﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolCode.Data.Linq; 
using CoolCode.Entities;

namespace CoolCode.Services
{
    public interface IService
    {
        void Submit(); 
    }

    public interface ITrackService:IService
    {
       void  LogAndSubmit(Log entity);
    }

    public  interface  ITrackService<T>  where T:ITrackEntity
    {
        IRepository<T> Repository { get;   }

        ITrackService Current { get; }
    }

    public static class ITrackServiceExtensions
    {
        public static T Get<T>(this ITrackService<T> service, int key) where T : ITrackEntity
        {
            return service.Repository.Get(c => c.ID == key );
        }

        public static bool Contains<T>(this ITrackService<T> service, int key) where T : ITrackEntity
        {
            return service.Repository.Contains(c => c.ID == key);
        }

        public static void Delete<T>(this ITrackService<T> service, int key) where T : ITrackEntity
        {
             service.Repository.Delete(c => c.ID == key);
             service.Current.Submit();
        }

        public static void Delete<T>(this ITrackService<T> service, int key, Log log) where T : ITrackEntity
        {
            service.Repository.Delete(c => c.ID == key);
            service.Current.LogAndSubmit(log);
        }
    }

}
